import 'bootstrap/dist/css/bootstrap.css';
import '../css/index2.scss';
require('bootstrap/dist/js/bootstrap.js');





require('./common.js');




$('#sidebar-menu a[href="index.html"]').addClass('active')
$('#sidebar-menu a[href="index.html"]').parent('li').addClass('active')



 require('./echartsFunction/chartClass.js');


 require('./echartsFunction/tool.js');




import { industryUrl } from './urlconfig/config.js';
import {getData, chartError, removeChartError} from './chartFunction.js'; //获取ajax请求返回的结果

let chart, resDataObj = {
	downloadData: [],
	otherPercentum: [],
	downloadPercentum: [],
	timeData: [],
};
var reqOption = {
	data: {
			time: 30,
        	page_number: 'all'
		},
	url: industryUrl,
	type: 'post'		
};

var chartData = {
	charSet: {
		yAxis: { 
			type: 'value', 
    	},
    	xAxis: {
			type: 'category',				    
		},
		
	},
	name: ['下载行业占比', '其他行业'],
	day: 30,
	type: 'bar',
	page: 'all'
}

function creat() {
	getData(reqOption).then(function(res){
		console.log();
		if(res.hasOwnProperty('items') || res.items.length > 0) {
			removeChartError($("#chart-content"))
			init(res.items);
			chart = new ChartClass($("#chart-content")[0], chartData);
			chart.init();
		}else {
			removeChartError($("#chart-content"))
			chartError($("#chart-content"))
		}
	}, function() {
		removeChartError($("#chart-content"))
		chartError($("#chart-content"))
		
	})
}

creat()

function init(resArr) {
	resDataObj.downloadData = [];
	resDataObj.downloadPercentum = [];
	resDataObj.otherPercentum = [];
	resDataObj.timeData = [];
	
	resArr.forEach(function(v) {
		if(v.hasOwnProperty('download')) {					
			resDataObj.downloadData.push(v.download);
			let downloadPercent = parseInt(v.download/(v.other+v.download) * 100);
			let otherPercent = 100 - downloadPercent;
		
			resDataObj.downloadPercentum.push(downloadPercent);
			
			resDataObj.otherPercentum.push(otherPercent);

			let time = tool.formatDate(v.time);
			
			resDataObj.timeData.push(time);
		}
	})
		
	chartData.data = resDataObj;
}


function setType(type, ele) {
	let ele_siblins = tool.sibling(ele);
	chartData.type = type;
	for(let index = 0, length = ele_siblins.length; index < length; index++) {
		tool.removeClass(ele_siblins[index], 'btn-default');
	}
	tool.addClass(ele, 'btn-default');
	chart.changeType(type);	
}
function setTime(dagnumber, ele) {
	reqOption.data.time = dagnumber;
	let ele_siblins = tool.sibling(ele);
	for(let index = 0, length = ele_siblins.length; index < length; index++) {
		tool.removeClass(ele_siblins[index], 'btn-default');
	}
	tool.addClass(ele, 'btn-default');
	chartData.day = dagnumber;
	creat()
	
}


function setPage(page, ele) {
	reqOption.data.page_number = page;
	let ele_siblins = tool.sibling(ele);
	for(let index = 0, length = ele_siblins.length; index < length; index++) {
		tool.removeClass(ele_siblins[index], 'btn-default');
	}
	tool.addClass(ele, 'btn-default');
	chartData.page = page;
	creat();
}

        		
window.setType  = setType;

window.setTime = setTime;
window.setPage = setPage;





